package com.ykh.mybbsdemo.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ykh.mybbsdemo.config.LogAnnotation;
import com.ykh.mybbsdemo.dao.mapper.VideoMapper;
import com.ykh.mybbsdemo.dao.pojo.dto.PageParams;
import com.ykh.mybbsdemo.dao.pojo.dto.VideoDto;
import com.ykh.mybbsdemo.dao.pojo.po.SysUser;
import com.ykh.mybbsdemo.dao.pojo.po.Video;
import com.ykh.mybbsdemo.dao.pojo.vo.ErrorCode;
import com.ykh.mybbsdemo.dao.pojo.vo.Result;
import com.ykh.mybbsdemo.service.VideoService;
import com.ykh.mybbsdemo.constant.VideoConstant;
import com.ykh.mybbsdemo.util.QiniuUtils;
import com.ykh.mybbsdemo.util.UserThreadLocal;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ykh
 * @since 2023年10月26日
 */
@RestController
@RequestMapping("/video")
public class
VideoController {
    @Autowired
    VideoService videoService;

    @Autowired
    private QiniuUtils qiniuUtils;

    @Autowired
    private VideoMapper videoMapper;


    @ApiOperation("上传视频")
    @PostMapping("/upload")
    public Result uploadVideo(VideoDto videoDto) {
        if(videoDto.getVideoFile() == null){
            return Result.fail(ErrorCode.VIDEO_UPLOAD_FAIL.getCode(), ErrorCode.VIDEO_UPLOAD_FAIL.getMsg());
        }
        return videoService.uploadVedio(videoDto);
    }

    @PostMapping("/video/image")
    @ApiOperation("上传视频封面图片")
    public Result upload(@RequestParam("image") MultipartFile file, @RequestParam Integer videoId){
        String fileName = UUID.randomUUID().toString() + "." +
                StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
        String key = qiniuUtils.upload(file, fileName);
        if (key!=null){
            String imageUrl = VideoConstant.IMAGE_URL + fileName;
            LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Video::getVideoId, videoId);
            //更新数据库中视频的封面地址
            Video video = videoMapper.selectOne(queryWrapper);
            if(video == null){
                return Result.fail(ErrorCode.NO_EXIST_VIDEO.getCode(),ErrorCode.NO_EXIST_VIDEO.getMsg());
            }
            video.setCoverUrl(imageUrl);
            videoMapper.update(video, queryWrapper);
            return Result.success(imageUrl);
        }
        return Result.fail(ErrorCode.VIDEO_IMG_UPLOAD_FAIL.getCode(), ErrorCode.VIDEO_IMG_UPLOAD_FAIL.getMsg());
    }

    @ApiOperation("获取单个视频")
    @PostMapping("/get")
    public Result getVideo(@RequestParam Integer videoId) throws UnsupportedEncodingException {
        return videoService.getVideo(videoId);
    }

    @PostMapping("/list")
    @ApiOperation("分页获取视频")
    public Result getVideoList(@RequestBody PageParams pageParams) {
        return Result.success(videoService.getVideoList(pageParams));
    }

    @PostMapping("/good")
    @ApiOperation("用户点赞视频")
    public Result goodVideo(@RequestParam Integer videoId) {
        return videoService.goodVideo(videoId);
    }

    /**
     * 根据用户id分页获取作品接口
     * @return
     */
    @ApiOperation("用户获取自己作品接口")
    @GetMapping("/info")
    public Result getUserVideoList(PageParams pageParams){
        SysUser user = UserThreadLocal.get();
        if(user == null){
            return Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());
        }
        return Result.success(videoService.getUserVideoList(pageParams, user.getAccount()));
    }

    /**
     * 根据分类分页获取作品接口
     * @return
     */
    @ApiOperation("根据分类分页获取作品接口")
    @GetMapping("/category")
    public Result getVideoListByCategory(PageParams pageParams, Integer categoryId){
        return Result.success(videoService.getVideoListByCategory(pageParams, categoryId));
    }

    /**
     * 获取视频总页数接口
     * @return
     */
    @ApiOperation("获取视频总页数接口")
    @GetMapping("/total/page")
//    @Cache(expire = 10*60*1000, name = "getVideoTotalPage")
    @LogAnnotation(module = "视频", operation = "获取视频分页的总页数")
    public Result getVideoTotalPage(PageParams pageParams){
        Long totalPage = videoService.getVideoTotalPage(pageParams);
        return Result.success(totalPage);
    }

}

